﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace QuanLyKho
{
    public partial class frmDanhSachTaiKhoan : Form
    {
        private TaiKhoan taiKhoan = new TaiKhoan("admin", "admin", "admin");
        private DataSet dsNhom, dsTaiKhoan;
        private string[] danhSachXoaNhom, danhSachXoaTaiKhoan;
        private int STTdanhSachXoaNhom, STTdanhSachXoaTaiKhoan;
        public frmDanhSachTaiKhoan()
        {
            InitializeComponent();
        }

        private void frmDanhSachTaiKhoan_Load(object sender, EventArgs e)
        {
            hienThiNhom();
            hienThiQuyen(0);
            hienThiTaiKhoan(gvNhom.GetDataRow(0)["tenLoaiTaiKhoan"].ToString());
        }

        private void hienThiNhom()
        {
            try
            {
                string sql = "select * from func_getDanhSachLoaiTaiKhoan()";
                dsNhom = connection.FillDataSet(sql, CommandType.StoredProcedure, taiKhoan);

                gctNhom.DataSource = dsNhom.Tables[0];
                gclTenNhom.FieldName = dsNhom.Tables[0].Columns["tenLoaiTaiKhoan"].ColumnName;
                danhSachXoaNhom = new string[dsNhom.Tables[0].Rows.Count];
                STTdanhSachXoaNhom = 0;
            }
            catch { }
        }

        private void hienThiTaiKhoan(string tenNhom)
        {
            try
            {
                string sql = "select * from func_getDanhSachTaiKhoanTheoLoai('" + tenNhom + "')";
                dsTaiKhoan = connection.FillDataSet(sql, CommandType.StoredProcedure, taiKhoan);
                
                gctTaiKhoan.DataSource = dsTaiKhoan.Tables[0];
                gclTenTaiKhoan.FieldName = dsTaiKhoan.Tables[0].Columns["ID"].ColumnName;
                danhSachXoaTaiKhoan = new string[dsTaiKhoan.Tables[0].Rows.Count];
                STTdanhSachXoaTaiKhoan = 0;
            }
            catch { }
        }

        private void hienThiQuyen(int viTriDong)
        {
            DataRow dr = dsNhom.Tables[0].Rows[viTriDong];
            int quyen = Convert.ToInt32(dr["quyen"].ToString());
            if (quyen >= 4)
            {
                chklQuyen.Items[0].CheckState = CheckState.Checked;
                quyen -= 4;
            }
            else
                chklQuyen.Items[0].CheckState = CheckState.Unchecked;
            
            if (quyen >= 2)
            {
                chklQuyen.Items[1].CheckState = CheckState.Checked;
                quyen -= 2;
            }
            else
                chklQuyen.Items[1].CheckState = CheckState.Unchecked;

            if (quyen >= 1)
            {
                chklQuyen.Items[2].CheckState = CheckState.Checked;
                quyen -= 1;
            }
            else
                chklQuyen.Items[2].CheckState = CheckState.Unchecked;

            if (chklQuyen.CheckedItems.Count == chklQuyen.Items.Count)
                chklChonHet.Items[0].CheckState = CheckState.Checked;
            else
                if (chklQuyen.CheckedItems.Count == 0)
                    chklChonHet.Items[0].CheckState = CheckState.Unchecked;
                else
                    chklChonHet.Items[0].CheckState = CheckState.Indeterminate;
            btnNapLaiQuyen.Visible = btnLuuQuyen.Visible = false;
        }

        private void gctNhom_MouseClick(object sender, MouseEventArgs e)
        {
            int[] itemChon = gvNhom.GetSelectedRows();
            if (!gclTenNhom.OptionsColumn.AllowEdit)
            {
                hienThiQuyen(itemChon[0]);
                hienThiTaiKhoan(gvNhom.GetDataRow(itemChon[0])["tenLoaiTaiKhoan"].ToString());
                if (e.Button == MouseButtons.Right)
                {
                    if (gvNhom.GetDataRow(itemChon[0])["tenLoaiTaiKhoan"].ToString().ToUpper().Trim() != "admin".ToUpper())
                    {
                        ContextMenu m = new ContextMenu();
                        m.MenuItems.Add(new MenuItem("&Chỉnh sửa", menuNhom_Sua));
                        m.MenuItems.Add(new MenuItem("&Xóa", menuNhom_Xoa));
                        m.Show(gctNhom, new Point(e.X, e.Y));
                    }
                }
            }
            
        }

        private void menuNhom_Sua(object sender, EventArgs e)
        {
            btnNapLaiNhom.Visible = btnLuuNhom.Visible = true;
            gvNhom.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gclTenNhom.OptionsColumn.AllowEdit = true;
        }

        private void menuNhom_Xoa(object sender, EventArgs e)
        {
            btnNapLaiNhom.Visible = btnLuuNhom.Visible = true;
            int[] itemChon = gvNhom.GetSelectedRows();
            danhSachXoaNhom[STTdanhSachXoaNhom] = gvNhom.GetDataRow(itemChon[0])["STT"].ToString();
            STTdanhSachXoaNhom++;
            gvNhom.DeleteRow(itemChon[0]);
        }

        private void btnSuaNhom_CheckedChanged(object sender, EventArgs e)
        {
            if (btnSuaNhom.Checked)
            {
                menuNhom_Sua(sender, e);
                btnSuaNhom.Checked = false;
            }
        }

        private void btnLuuNhom_CheckedChanged(object sender, EventArgs e)
        {
            btnNapLaiNhom.Visible = btnLuuNhom.Visible = false;
            gvNhom.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gclTenNhom.OptionsColumn.AllowEdit = false;
            capNhatNhom(dsNhom.Tables[0]);
            hienThiNhom();
        }

        private void capNhatNhom(DataTable dt)
        {
            if (dt.GetChanges() != null)
            {
                for (int i = 0; i < danhSachXoaNhom.Length; i++)
                {
                    if (danhSachXoaNhom[i] != null)
                    {
                        string[] para = { "@STT" };
                        string[] values = { danhSachXoaNhom[i] };

                        connection.Excute("proc_deleteDanhMucTaiKhoan", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                }

                foreach (DataRow dr in dt.GetChanges().Rows)
                {
                    if (dr.RowState == DataRowState.Added)
                    {
                        string[] para = { "@TenLoaiTaiKhoan", "@PhanQuyen" };
                        string[] values = { dr["tenLoaiTaiKhoan"].ToString(), "0" };

                        connection.Excute("proc_insertDanhMucTaiKhoan", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                    else
                        if (dr.RowState == DataRowState.Modified)
                        {
                            if (dr["STT"].ToString() != "0")
                            {
                                string[] para = { "@STT", "@TenLoaiTaiKhoanMoi", "@PhanQuyen" };
                                string[] values = { dr["STT"].ToString(), dr["tenLoaiTaiKhoan"].ToString(), "-1" };

                                connection.Excute("proc_updateDanhMucTaiKhoan", CommandType.StoredProcedure, para, values, taiKhoan);
                            }
                        }
                    
                }
            }
        }

        private void btnNhapLaiNhom_CheckedChanged(object sender, EventArgs e)
        {
            if (btnNapLaiNhom.Checked)
            {
                btnNapLaiNhom.Visible = btnLuuNhom.Visible = false;
                gvNhom.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gclTenNhom.OptionsColumn.AllowEdit = false;
                hienThiNhom();
                btnNapLaiNhom.Checked = false;
            }
        }

        private void btnXoaNhom_CheckedChanged(object sender, EventArgs e)
        {
            if (btnXoaNhom.Checked)
            {
                menuNhom_Xoa(sender, e);
                btnXoaNhom.Checked = false;
            }
        }

        private void chklChonHet_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (chklChonHet.Items[0].CheckState == CheckState.Checked)
                chklQuyen.CheckAll();
            else
                if (chklChonHet.Items[0].CheckState == CheckState.Unchecked)
                    chklQuyen.UnCheckAll();
        }

        private void chklQuyen_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (chklQuyen.CheckedItems.Count == chklQuyen.Items.Count)
                chklChonHet.Items[0].CheckState = CheckState.Checked;
            else
                if (chklQuyen.CheckedItems.Count == 0)
                    chklChonHet.Items[0].CheckState = CheckState.Unchecked;
                else
                    chklChonHet.Items[0].CheckState = CheckState.Indeterminate;
            btnNapLaiQuyen.Visible = btnLuuQuyen.Visible = true;
            btnNapLaiQuyen.Checked = btnLuuQuyen.Checked = false;
        }

        private void btnNapLaiQuyen_CheckedChanged(object sender, EventArgs e)
        {
            if (btnNapLaiQuyen.Checked)
            {
                int[] selectItem = gvNhom.GetSelectedRows();
                hienThiQuyen(selectItem[0]);
                btnNapLaiQuyen.Visible = btnLuuQuyen.Visible = false;
            }
        }

        private void btnLuuQuyen_CheckedChanged(object sender, EventArgs e)
        {
            if (btnLuuQuyen.Checked)
            {
                int phanQuyen = 0;
                int[] selectItem = gvNhom.GetSelectedRows();

                if (chklQuyen.Items[0].CheckState == CheckState.Checked)
                    phanQuyen += 4;
                if (chklQuyen.Items[1].CheckState == CheckState.Checked)
                    phanQuyen += 2;
                if (chklQuyen.Items[2].CheckState == CheckState.Checked)
                    phanQuyen += 1;

                DataRow dr = dsNhom.Tables[0].Rows[selectItem[0]];
                string[] para = { "@STT", "@TenLoaiTaiKhoanMoi", "@PhanQuyen" };
                string[] values = { dr["STT"].ToString(), "" , phanQuyen.ToString() };

                connection.Excute("proc_updateDanhMucTaiKhoan", CommandType.StoredProcedure, para, values, taiKhoan);
                hienThiNhom();
                btnNapLaiQuyen_CheckedChanged(sender, e);
            }
        }

        private void btnTehmTaiKhoan_CheckedChanged(object sender, EventArgs e)
        {
            if (btnThemTaiKhoan.Checked)
            {
                menuTaiKhoan_Them(sender, e);
                btnThemTaiKhoan.Checked = false;
            }
        }

        private void menuTaiKhoan_Them(object sender, EventArgs e)
        {
            btnNapLaiTaiKhoan.Visible = btnLuuTaiKhoan.Visible = true;
            gvTaiKhoan.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gclTenTaiKhoan.OptionsColumn.AllowEdit = true;
        }

        private void menuTaiKhoan_Xoa(object sender, EventArgs e)
        {
            btnNapLaiTaiKhoan.Visible = btnLuuTaiKhoan.Visible = true;
            int[] selectItem = gvTaiKhoan.GetSelectedRows();
            danhSachXoaTaiKhoan[STTdanhSachXoaTaiKhoan] = gvTaiKhoan.GetDataRow(selectItem[0])["STT"].ToString();
            STTdanhSachXoaTaiKhoan++;
            gvTaiKhoan.DeleteRow(selectItem[0]);
        }

        private void gctTaiKhoan_MouseClick(object sender, MouseEventArgs e)
        {
            if (!gclTenTaiKhoan.OptionsColumn.AllowEdit)
            {
                if (e.Button == MouseButtons.Right)
                {
                    ContextMenu m = new ContextMenu();
                    m.MenuItems.Add(new MenuItem("&Xóa",menuTaiKhoan_Xoa));
                    m.Show(gctTaiKhoan, new Point(e.X, e.Y));
                }
            }
        }

        private void btnXoaTaiKhoan_CheckedChanged(object sender, EventArgs e)
        {
            if (btnXoaTaiKhoan.Checked)
            {
                menuTaiKhoan_Xoa(sender, e);
                btnXoaTaiKhoan.Checked = false;
            }
        }

        private void btnNapLaiTaiKhoan_CheckedChanged(object sender, EventArgs e)
        {
            if (btnNapLaiTaiKhoan.Checked)
            {
                btnNapLaiTaiKhoan.Visible = btnLuuTaiKhoan.Visible = false;
                gvTaiKhoan.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gclTenTaiKhoan.OptionsColumn.AllowEdit = false;
                hienThiTaiKhoan(gvNhom.GetDataRow(0)["tenLoaiTaiKhoan"].ToString());
                btnNapLaiTaiKhoan.Checked = false;
            }
        }

        private void btnLuuTaiKhoan_CheckedChanged(object sender, EventArgs e)
        {
            if (btnLuuTaiKhoan.Checked)
            {
                capNhatTaiKhoan(dsTaiKhoan.Tables[0]);
                btnNapLaiTaiKhoan.Checked = true;
                btnLuuTaiKhoan.Checked = false;
            }
        }

        private void capNhatTaiKhoan(DataTable dt)
        {
            int[] itemChon = gvNhom.GetSelectedRows();
            if (dt.GetChanges() != null)
            {
                for (int i = 0; i < danhSachXoaNhom.Length; i++)
                {
                    if (danhSachXoaNhom[i] != null)
                    {
                        string[] para = { "@STT" };
                        string[] values = { danhSachXoaNhom[i] };

                        connection.Excute("proc_deleteDanhMucTaiKhoan", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                }

                foreach (DataRow dr in dt.GetChanges().Rows)
                {
                    
                    if (dr.RowState == DataRowState.Added)
                    {
                        string[] para = { "@ID", "@STTLoaiTaiKhoan" };
                        string[] values = { dr["ID"].ToString(), dsNhom.Tables[0].Rows[itemChon[0]]["STT"].ToString() };

                        connection.Excute("proc_insertUsers", CommandType.StoredProcedure, para, values, taiKhoan);
                    }
                    else
                        if (dr.RowState == DataRowState.Modified)
                        {
                            if (dr["STT"].ToString() != "0")
                            {
                                string[] para = { "@STT", "@TenLoaiTaiKhoanMoi", "@PhanQuyen" };
                                string[] values = { dr["STT"].ToString(), dr["tenLoaiTaiKhoan"].ToString(), "-1" };

                                connection.Excute("proc_updateDanhMucTaiKhoan", CommandType.StoredProcedure, para, values, taiKhoan);
                            }
                        }
                }
            }
        }
    }
}
